package breeze.pixel.weather.apps_util.views;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import breeze.pixel.weather.App;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;

public class AirWidgetView extends SurfaceView implements SurfaceHolder.Callback {
    public AirWidgetView(Context context) {
        this(context,null);
    }

    public AirWidgetView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private Paint barPaint,proPaint;
    private Handler thread;
    private Context context;
    private AirWidgetReceiver airWidgetReceiver;
    private void init(Context context) {
        this.context = context;
        barPaint = new Paint();
        barPaint.setStrokeWidth(20);
        barPaint.setStyle(Paint.Style.STROKE);
        barPaint.setColor(Color.parseColor("#f5f5f5"));
        barPaint.setAntiAlias(true);

        proPaint = new Paint();
        proPaint.setStrokeWidth(20);
        proPaint.setStyle(Paint.Style.STROKE);
        proPaint.setColor(Color.parseColor("#EF6C00"));
        proPaint.setAntiAlias(true);

        setZOrderOnTop(true);
        getHolder().addCallback(this);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        initBg();
        IntentFilter intentFilter = new IntentFilter("breeze.pixel.weather.action.target.AppLinearLayout");
        airWidgetReceiver = new AirWidgetReceiver();
        context.registerReceiver(airWidgetReceiver,intentFilter);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        context.unregisterReceiver(airWidgetReceiver);
    }

    class AirWidgetReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            initBg();
        }
    }

    private void initBg(){
        AppSettings appSettings = App.getAppSettings();
        setBackgroundColor(Color.TRANSPARENT);
    }

    public AirWidgetView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
        invalidate();
    }

    private float width,height;
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    private int value;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(width/2,height/2);
        if (value<=50){
            proPaint.setColor(Color.parseColor("#1fab89"));
        }else if (value>50 && value<=100){
            proPaint.setColor(Color.parseColor("#fbdd3d"));
        }else if (value>100 && value<=150){
            proPaint.setColor(Color.parseColor("#EF6C00"));
        }else if (value>150 && value<=200){
            proPaint.setColor(Color.parseColor("#F44336"));
        }else if (value>200 && value<=300){
            proPaint.setColor(Color.parseColor("#AB47BC"));
        }else if (value>300){
            proPaint.setColor(Color.BLACK);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawArc(-width/2.5f,-height/4,width/2.5f,height,-180,180,false,barPaint);
            canvas.drawArc(-width/2.5f,-height/4,width/2.5f,height,-180,value/1.5f,false,proPaint);
        }
    }
}
